package com.meitianhui.convenitenbuy.module.mine.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.module.mine.adapter.CollectStoreAdapter;
import com.meitianhui.convenitenbuy.module.mine.contract.CollectionContract;
import com.meitianhui.convenitenbuy.module.mine.di.DaggerMineComponent;
import com.meitianhui.convenitenbuy.module.mine.di.MineModule;
import com.meitianhui.convenitenbuy.module.mine.model.CollectedStoresBean;
import com.meitianhui.convenitenbuy.module.mine.model.rxbusBean.CollectStoreEventBean;
import com.meitianhui.convenitenbuy.module.mine.presenter.CollectionPresenter;
import com.meitianhui.convenitenbuy.module.store.ui.activity.StoreDetailActivity;
import com.meitianhui.convenitenbuy.util.RxBus;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by amy on 2017/11/18.
 */

public class CollectionActivity extends BaseToolbarActivity implements CollectionContract.View {

    private RecyclerView mRvCollect;
    private TextView mTvCollectCancle;
    private TextView mTvCollectDefault;
    private LinearLayout mLlCollectButton, mLlCollectNone;
    private List<CollectedStoresBean.Stores> mStoresList = new ArrayList<>();
    private List<CollectedStoresBean.Stores> mListSelectTemp = new ArrayList<>();


    @Inject
    CollectionPresenter mCollectionPresenter;
    private CollectStoreAdapter mCollectStoreAdapter;
    private HashMap<String, String> mDoc_url = new HashMap<>();

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerMineComponent.builder().mineModule(new MineModule(this)).appComponent(App.getInstance().getAppComponent()).build().inject(this);
        isEnable(false);
        setTbTitle("收藏门店");
        setTbMenu("管理");
        initId();
        initRXClick();
        initData();
     //   getDataFromServer();

    }

    private void initData() {
        mCollectStoreAdapter = new CollectStoreAdapter(mStoresList, mDoc_url, this);
        mCollectStoreAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //点击条目信息
                if (isChooseManager) {
                    //跳转到店铺信息页面
                    Intent intent = new Intent(CollectionActivity.this, StoreDetailActivity.class);
                    intent.putExtra("store_id", mStoresList.get(position).stores_id);
                    intent.putExtra("store_name",mStoresList.get(position).stores_name);
                    intent.putExtra("store_address",mStoresList.get(position).address);
                    startActivity(intent);
                } else {
                    //设置默认门店还是取消收藏
                    mStoresList.get(position).ifChoose = !mStoresList.get(position).ifChoose;
                    if (mListSelectTemp.contains(mStoresList.get(position))) {
                        mListSelectTemp.remove(mStoresList.get(position));
                    } else {
                        mListSelectTemp.add(mStoresList.get(position));
                    }
                    //刷新单个条目
                    mCollectStoreAdapter.notifyItemChanged(position);
                    //根据mListSelectTemp的大小设置两个button的状态
                    if (mListSelectTemp.size() == 0) {
                        //都变成不可点击
                        mTvCollectDefault.setClickable(false);
                        mTvCollectCancle.setClickable(false);
                        mTvCollectDefault.setTextColor(getResources().getColor(R.color.button_text_unclick));
                        mTvCollectCancle.setTextColor(getResources().getColor(R.color.button_text_unclick));
                    } else if (mListSelectTemp.size() == 1) {
                        //都变成可点击
                        mTvCollectDefault.setClickable(true);
                        mTvCollectCancle.setClickable(true);
                        mTvCollectDefault.setTextColor(getResources().getColor(R.color.colorAccent));
                        mTvCollectCancle.setTextColor(getResources().getColor(R.color.colorAccent));
                    } else {
                        //只显示取消收藏，默认改为不可点击
                        mTvCollectDefault.setClickable(false);
                        mTvCollectCancle.setClickable(true);
                        mTvCollectDefault.setTextColor(getResources().getColor(R.color.button_text_unclick));
                        mTvCollectCancle.setTextColor(getResources().getColor(R.color.colorAccent));
                    }
                }
            }
        });
        LinearLayoutManager manager = new LinearLayoutManager(this);
        mRvCollect.setLayoutManager(manager);
        mRvCollect.setAdapter(mCollectStoreAdapter);

    }

    private void initRXClick() {
        RxUtils.bindEvents(mTvCollectCancle, this);
        RxUtils.bindEvents(mTvCollectDefault, this);
    }

    private void initId() {
        mLlCollectButton = getViewById(R.id.ll_collect_button);
        mTvCollectCancle = getViewById(R.id.tv_collect_cancle);
        mTvCollectDefault = getViewById(R.id.tv_collect_default);
        mRvCollect = getViewById(R.id.rv_collect);
        mLlCollectNone = getViewById(R.id.ll_collect_none);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getDataFromServer();
    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()) {
            case R.id.tv_collect_cancle:
                //取消收藏
                String mCollectStoresId = "";
                for (int i = 0; i < mListSelectTemp.size(); i++) {
                    mCollectStoresId += mListSelectTemp.get(i).stores_id + ",";

                }
                if (!TextUtils.isEmpty(mCollectStoresId) && mListSelectTemp.size() != 0) {
                    mCollectStoresId = mCollectStoresId.substring(0, mCollectStoresId.length() - 1);
                }
                mCollectionPresenter.deleteStoreCollect(mCollectStoresId);
                break;
            case R.id.tv_collect_default:
                //设为默认店铺
                if (mListSelectTemp.size() == 1) {
                    //只能有一个默认店铺
                    mCollectionPresenter.setDefaultStore(mListSelectTemp.get(0).stores_id);
                }
                break;
        }
    }

    @Override
    protected int setView() {
        return R.layout.activity_collection;
    }

    @Override
    protected void onRefreshData() {

    }

    /**
     * 菜单按钮的点击事件
     */
    boolean isChooseManager = true;

    @Override
    protected void clickMenu() {
        super.clickMenu();
        System.out.println(mListSelectTemp.size()+"==============");
        if (isChooseManager) {
            if (mListSelectTemp.size() != 0) {
                for (int i = 0; i < mStoresList.size(); i++) {
                    //主要是解决编辑界面有选择条目但没有设置时的问题
                  //  if (mListSelectTemp.contains(mStoresList.get(i))) {
                        mStoresList.get(i).ifChoose = false;
                  //  }
                }
            }
            setTbMenu("取消");
            isChooseManager = false;
            //显示底部的两个button
            mLlCollectButton.setVisibility(View.VISIBLE);

            RxBus.get().post(new CollectStoreEventBean(Constants.COLLECT_MENU_CANCLE));

            //防止二次进来，没有选中item时，下面两个Button也变成可点击
            mTvCollectDefault.setClickable(false);
            mTvCollectCancle.setClickable(false);
            mTvCollectDefault.setTextColor(getResources().getColor(R.color.button_text_unclick));
            mTvCollectCancle.setTextColor(getResources().getColor(R.color.button_text_unclick));
        } else {
            if (mListSelectTemp.size() != 0) {
                for (int i = 0; i < mStoresList.size(); i++) {
                    //主要是解决编辑界面有选择条目但没有设置时的问题
                    //if (mListSelectTemp.contains(mStoresList.get(i))) {
                        mStoresList.get(i).ifChoose = false;
                   // }
                }
            }

            setTbMenu("管理");
            isChooseManager = true;
            //隐藏两个按钮
            mLlCollectButton.setVisibility(View.GONE);

            RxBus.get().post(new CollectStoreEventBean(Constants.COLLECT_MENU_COMFIRM));
        }
        mListSelectTemp.clear();
        mCollectStoreAdapter.notifyDataSetChanged();
    }

    public void getDataFromServer() {
        mCollectionPresenter.getUserCollectionStore();

    }

    @Override
    public void showError(String msg) {
        TipUtils.showToast(App.getInstance(), msg, 3);
    }

    @Override
    public void showEmpty() {
        mLlCollectNone.setVisibility(View.VISIBLE);
        mRvCollect.setVisibility(View.GONE);
       // mCollectStoreAdapter.setNewData(mListSelectTemp);
        setTbMenu("");
    }

    /**
     * 显示收藏列表
     *
     * @param collectStoreBean
     */
    @Override
    public void showCollection(CollectedStoresBean collectStoreBean) {
        mLlCollectNone.setVisibility(View.GONE);
        mRvCollect.setVisibility(View.VISIBLE);
        mStoresList = collectStoreBean.list;
        mDoc_url = collectStoreBean.doc_url;
        mCollectStoreAdapter.setDoc_url(mDoc_url);
        mCollectStoreAdapter.setNewData(mStoresList);

    }

    @Override
    public void upDataSuccess() {
        mListSelectTemp.clear();
        TipUtils.showToast(App.getInstance(), "更新成功", 3);
        //再拉取一次服务器数据看是否为空
        mCollectionPresenter.getUserCollectionStore();
        //回到管理的menu界面
        setTbMenu("管理");
        isChooseManager = true;
        //隐藏两个按钮
        mLlCollectButton.setVisibility(View.GONE);

        RxBus.get().post(new CollectStoreEventBean(Constants.COLLECT_MENU_COMFIRM));

    }
}
